package com.lkl.demo.provider.controller;

import com.lkl.demo.common.constant.RabbitMQConstant;
import com.lkl.demo.provider.bean.Orders;
import com.lkl.demo.provider.mapper.OrdersMapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 新增订单
     */
    @PostMapping("/add")
    public String add() {
        Orders orders = new Orders();
        orders.setName("banana");
        orders.setType("0");
        ordersMapper.insert(orders);

        // 发消息至mq
        HashMap<String, String> map = new HashMap<>();
        map.put("orderId", orders.getId());
        rabbitTemplate.convertAndSend(RabbitMQConstant.DELAY_DEAD_EXCHANGE, RabbitMQConstant.ROUTER_KEY_DELAY, map);
        return "success";
    }
}
